package com.supor.aiot.mine.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.utils.BitmapUtil;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.ImageItem;
import com.supor.aiot.mine.helper.FeedbackImageCache;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: ImageGridAdapter <br>
 * 作者: lei <br>
 * 日期: 2021/2/1 <br>
 */
public class ImageGridAdapter extends BaseAdapter {
    private List<ImageItem> dataList;
    private Handler mHandler;
    private Map<String, ImageItem> map;
    private int selectTotal;
    private TextCallback textcallback;
    int screenWidth;
    private Context context;
    private int maxNum;

    public ImageGridAdapter(Context context, final List<ImageItem> dataList, final Handler mHandler) {
        this.context = context;
        this.textcallback = null;
        this.map = new HashMap<>();
        this.selectTotal = 0;
        this.dataList = dataList;
        this.mHandler = mHandler;
        DisplayMetrics dm = AppAgent.getApp().getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
    }

    @Override
    public int getCount() {
        if (this.dataList == null) {
            return 0;
        }
        return this.dataList.size();
    }

    @Override
    public Object getItem(final int n) {
        return null;
    }

    @Override
    public long getItemId(final int n) {
        return n;
    }

    public Map<String, ImageItem> getMap() {
        return this.map;
    }

    @Override
    public View getView(final int n, View inflate, final ViewGroup viewGroup) {
        final Holder holder;
        if (inflate == null) {
            final Holder tag = new Holder();
            inflate = View.inflate(viewGroup.getContext(), R.layout.item_image_grid, null);
            tag.iv = (ImageView) inflate.findViewById(R.id.image);
            tag.selected = (ImageView) inflate.findViewById(R.id.isselected);
            tag.ivPlay = (ImageView) inflate.findViewById(R.id.iv_play);

            if (screenWidth > 0) {
                ViewGroup.LayoutParams params = tag.iv.getLayoutParams();
                //设置图片的相对于屏幕的宽高比
                params.width = screenWidth / 4;
                params.height = screenWidth / 4;
                tag.iv.setLayoutParams(params);
            }
            inflate.setTag(tag);
            holder = tag;
        } else {
            holder = (Holder) inflate.getTag();
        }
        final ImageItem imageItem = this.dataList.get(n);
        BitmapUtil.loadLocal(context, imageItem.imagePath, holder.iv);
        if (imageItem.isSelected) {
            holder.selected.setImageResource(R.mipmap.checkbox_bg_checked);
        } else {
            holder.selected.setImageResource(R.mipmap.checkbox_bg_unchecked);
        }
        if (imageItem.type == 0) {
            holder.ivPlay.setVisibility(View.GONE);
        } else {
            holder.ivPlay.setVisibility(View.VISIBLE);
        }
        holder.iv.setOnClickListener(view -> {
            boolean isSelected = true;
            final String imagePath = dataList.get(n).imagePath;
            if (FeedbackImageCache.getInstance().getDrr().size() + selectTotal < maxNum) {
                imageItem.isSelected = !imageItem.isSelected;
                if (!imageItem.isSelected) {
                    holder.selected.setImageResource(R.mipmap.checkbox_bg_unchecked);
                    selectTotal--;
                    if (textcallback != null) {
                        textcallback.onListen(selectTotal);
                    }
                    getMap().remove(imagePath);
                    return;
                }
                holder.selected.setImageResource(R.mipmap.checkbox_bg_checked);
                selectTotal++;
                if (textcallback != null) {
                    textcallback.onListen(selectTotal);
                }
                getMap().put(imagePath, imageItem);
            } else if (FeedbackImageCache.getInstance().getDrr().size() + selectTotal >= maxNum) {
                if (imageItem.isSelected) {
                    isSelected = false;
                    imageItem.isSelected = isSelected;
                    holder.selected.setImageResource(R.mipmap.checkbox_bg_unchecked);
                    selectTotal--;
                    getMap().remove(imagePath);
                    return;
                }
                Message.obtain(mHandler, 0).sendToTarget();
            }
        });
        return inflate;
    }

    public void notifyData(final List<ImageItem> dataList) {
        this.dataList = dataList;
        this.notifyDataSetChanged();
    }

    public void setMap(final Map<String, ImageItem> map) {
        this.map = map;
    }

    public void setTextCallback(final TextCallback textcallback) {
        this.textcallback = textcallback;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    private static class Holder {
        private ImageView iv;
        private ImageView selected;
        private ImageView ivPlay;
    }

    public interface TextCallback {
        void onListen(final int p0);
    }
}
